import { ApiResponse } from "@bigwinepot/api_manager/src/main/ets/api/ApiResponse";
import { HttpApiManager } from "@bigwinepot/api_manager/src/main/ets/api/HttpApiManager";
import { HttpInterceptor } from "@bigwinepot/api_manager/src/main/ets/api/RequestOptions";
import { http } from "@kit.NetworkKit";

/**
 * Api业务封装类，主要用于常规 API 请求调用
 */
export class ApiBusinessManager {
  private apiManager: HttpApiManager;
  private static instance: ApiBusinessManager;

  /**
   * 单例
   * @param isSingle
   * @returns
   */
  public static getInstance(): ApiBusinessManager {

    if (!ApiBusinessManager.instance) {
      ApiBusinessManager.instance = new ApiBusinessManager();
    }
    return ApiBusinessManager.instance;
  }


  constructor() {
    // 获取 HttpApiManager 单例实例
    this.apiManager = HttpApiManager.getInstance();
  }

  /**
   * 设置请求的基础URL
   * @param baseUrl
   */
  public setBaseUrl(baseUrl: string): ApiBusinessManager {
    this.apiManager.setBaseUrl(baseUrl);
    return this;
  }

  /**
   * 设置最大重试次数
   * @param retries
   */
  public setMaxRetries(retries: number): ApiBusinessManager {
    this.apiManager.setMaxRetries(retries);
    return this;
  }

  /**
   * 设置请求超时时间（毫秒）
   * @param milliseconds
   */
  public setTimeout(milliseconds: number): ApiBusinessManager {
    this.apiManager.setTimeout(milliseconds);
    return this;
  }

  /**
   * 设置请求的 User-Agent
   * @param agent
   */
  public setAgent(agent: string): ApiBusinessManager {
    this.apiManager.setAgent(agent);
    return this;
  }

  /**
   * 设置请求头 Content-Type
   * @param contentType
   */
  public setContentType(contentType: string): ApiBusinessManager {
    this.apiManager.setContentType(contentType);
    return this;
  }

  /**
   * 设置请求头 Accept
   * @param accept
   */
  public setAccept(accept: string): ApiBusinessManager {
    this.apiManager.setAccept(accept);
    return this;
  }

  /**
   * 设置期望的数据类型（例如：对象、数组、二进制数据等）
   * @param expectDataType
   */
  public setExpectDataType(expectDataType: http.HttpDataType): ApiBusinessManager {
    this.apiManager.setExpectDataType(expectDataType);
    return this;
  }

  /**
   * 设置响应格式的对象字段映射
   * @param respObjectFormat
   */
  public setRespObjectFormat(respObjectFormat: { codeKeyName: string; meesageKeyName: string; dataKeyName: string }): ApiBusinessManager {
    this.apiManager.setRespObjectFormat(respObjectFormat);
    return this;
  }

  /**
   * 添加请求头
   * @param key
   * @param value
   */
  public addHeader(key: string, value: string): ApiBusinessManager {
    this.apiManager.addHeader(key, value);
    return this;
  }

  /**
   * 移除请求头
   * @param key
   */
  public removeHeader(key: string): ApiBusinessManager {
    this.apiManager.removeHeader(key);
    return this;
  }

  /**
   * 添加请求拦截器
   * @param interceptor
   */
  public addInterceptor(interceptor: HttpInterceptor): ApiBusinessManager {
    this.apiManager.addInterceptor(interceptor);
    return this;
  }

  /**
   * 执行 GET 请求
   * @param url
   */
  public async get(url: string): Promise<ApiResponse> {
    try {
      const response = await this.apiManager.get(url);
      return response;
    } catch (error) {
      return new ApiResponse(error.code, error.message, null);
    }
  }

  /**
   * 执行 POST 请求
   * @param url
   * @param data
   */
  public async post(url: string, data?: any): Promise<ApiResponse> {
    try {
      const response = await this.apiManager.post(url, data);
      return response;
    } catch (error) {
      return new ApiResponse(error.code, error.message, null);
    }
  }

  /**
   * 执行 PUT 请求
   * @param url
   * @param data
   */
  public async put(url: string, data?: any): Promise<ApiResponse> {
    try {
      const response = await this.apiManager.put(url, data);
      return response;
    } catch (error) {
      return new ApiResponse(error.code, error.message, null);
    }
  }

  /**
   * 执行 DELETE 请求
   * @param url
   */
  public async delete(url: string): Promise<ApiResponse> {
    try {
      const response = await this.apiManager.delete(url);
      return response;
    } catch (error) {
      return new ApiResponse(error.code, error.message, null);
    }
  }
}
